﻿package ygg.se
{
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.FileFilter;
	import flash.net.registerClassAlias;
	import flash.utils.describeType;
	import flash.xml.XMLDocument;
	import mx.graphics.SolidColor;
	
	import flash.filesystem.File;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import flash.net.SharedObject;
	import flash.net.ObjectEncoding;
	
	import ygg.filetype.sol.*;
	import ygg.test.*;
	import j.sus2maj7.util.*;
	
	public class YggSE extends Sprite
	{
		
		
		public function YggSE()
		{
			trace('YggSE');
			registerClassAlias("TC", TestClass);
			registerClassAlias("TDC", TestDynClass);
			writeSamepleSo();
			
			/*
			var tc:TestClass = new TestClass();
			tc.aa = 'ring';
			tc.bb = 'long';
			
            var ba:ByteArray = new ByteArray();
            ba.writeObject(tc);
            ba.position = 0;
			ba.readObject();
			
			trace( ByteUtil.print(ba) );
			*/
			
			FileUtil.browse(new FileFilter("sol", "*.sol"), "Open .sol File", onSelectSolFile);
			
		}
		
		private function writeSamepleSo():void
		{
			trace('writeSamepleSo');
			
			 var so:SharedObject = SharedObject.getLocal("testsolfilewithlongname");
			
			 so.clear();
			
			var i2_28:int = -268435456;
			var u2_28:uint = 268435456;
			var i2_28_1:int = -268435455;
			var u2_28_1:uint = 268435455;
			var i2_29:int = -536870912;
			var u2_29:uint = 536870912;
			var i2_29_1:int = -536870911;
			var u2_29_1:uint = 536870911;
			
			 //so.data.u291 = -0x7F; //127
			 //so.data.u292n = -0x3FFF; //16383
			 //so.data.u292p = 0x4001; //16383
			 //so.data.u293 = -0x1FFFFF; //2097151
			 //so.data.u294 = 0x3FFFFFFF; //1073741823
			// so.data.u29x = 0x40000000; //ERROR - double
			//*
			so.data.bigInt3 = 5;
			so.data.bigInt4 = u2_28_1;
			 so.data.myFloat = Math.PI;
			 so.data.smallInt2 = 0x7F;
			 so.data.smallInt2 = 9;
			 so.data.bigInt3 = u2_29_1;
			so.data.myString = 'jason';
			 so.data.myFalse = false;
			 so.data.myTrue = true;
			 so.data.myNull = null;
			 so.data.myUnd = undefined;
			//*/
			//*
			 so.data.myDate = new Date();
			 so.data.myXmlDoc = new XMLDocument("<start><p>test</p><p>test2</p></start>");
			 so.data.myXML = new XML("<start><p>test</p><p>test2</p></start>");
			
			 var myBa:ByteArray = new ByteArray();
			myBa.writeInt(7043);
			 so.data.myBa = myBa;
			//*/
			//*
			 so.data.myIntArray = [1,2,3];
			 so.data.myStringArray = ['eins','zwei'];
			//*/
			//*
			
			var tc:TestClass = new TestClass();
			tc.aa = 'ring';
			tc.bb = 'long';
			tc.dd = 'kang';
			so.data.customClass = tc;
			so.data.customClass2 = so.data.customClass;
			//*
			var tc2:TestClass = new TestClass();
			tc2.aa = 'ring';
			tc2.bb = 'long';
			so.data.customClass3 = tc2;
			//*/
			//*
			var tdc:TestDynClass = new TestDynClass();
			tdc.aa = 'ring';
			tdc.bb = 'long';
			tdc.cc = 'kang';
			so.data.customDClass = tdc;
			//so.data.customDClass2 = so.data.customDClass;
			var tdc2:TestDynClass = new TestDynClass();
			tdc2.aa = 'ring';
			tdc2.bb = 'long';
			tdc2.cc = 'kang';
			so.data.customDClass3 = tdc2;
			//*/
			//*
			 so.data.smallInt2 = 9;
			 so.data.bigInt2 = 1025;
			 so.data.myString2 = 'jason';
			 so.data.myFalse2 = false;
			 so.data.myTrue2 = true;
			 so.data.myNull2 = null;
			 so.data.myUnd2 = undefined;
			 so.data.myFloat2 = Math.PI;
			 so.data.myDate2 = new Date();
			 so.data.myXmlDoc2 = so.data.myXmlDoc;
			 so.data.myXML2 = so.data.myXML;
			
			 so.data.myBa2 = myBa;
			
			 so.data.myIntArray2 = [1,2,3];
			 so.data.myStringArray2 = ['eins','zwei'];
			//*/
			//*
			so.data.myObject21 = {p3: 'hallo', p4: 8};
			so.data.myObject23 = {p3: 'hallo', p4: 8};
			
			so.data.myObject22 = so.data.myObject21;
			//*
			so.data.myObject1 = { aa: 5, bb: 6 };
			so.data.myObject2 = so.data.myObject1;
			so.data.myObject3 = {cc: 7 , aa: 9, bb:3 };
			
			var myArr:Array = [1, 2, 3];
			myArr[7] = 5;
			myArr["tee"] = 9;
			so.data.myArr = myArr;
			
			trace(myArr, myArr[7], myArr["tee"]);
			//*/
			
			so.flush();

		}
		
		
		private function onSelectSolFile(e:Event):void
		{
			trace ("onSelectFile");
			
			loadSolFile(File(e.target));
			
			
		}
		
		
		private function loadSolFile(file:File):void
		{
			if (file == null || !file.exists)
			{
				trace ("doesn't exist");
				return;
			}
			
			try
			{
				trace( 'read');
				var ba:ByteArray = FileUtil.read(file);
				//bytes.endian = Endian.LITTLE_ENDIAN;
				
				var sol:Sol = new Sol();
				sol.setBytes(ba);
				
				trace(sol.binary.toString());
				sol.decode();
				
				var tc:TestClass = new TestClass();
				tc.aa = 'ring';
				tc.bb = 'long';
				/*
				sol.body = { };
				sol.addObject('cack', tc);
				
				var tdc:TestDynClass = new TestDynClass();
				tdc.aa = 'ring';
				tdc.bb = 'long';
				tdc.cc = 'kang';
				sol.addObject('casck', tdc);
				//tc.cc = 'kang';
				/*
				trace( describeType(tc) );
				sol.body.customClass = tc;
				trace( describeType(sol.body.customClass) );
				t.obj(sol.body.customClass);
				sol.toString();
				//*/
				trace('\n');
				sol.encode();
				
				trace(sol.binary.toString());
				sol.decode();
				
				//trace(sol.binary.toString());
				//t.DoNotBroadcast = true;
				//t.obj(sol.body);
			}
			catch (error:Error)
			{
				trace( 'error');
				
				trace (error);
				trace (error.getStackTrace());
				
				return;
			}
		}
		
	
	}
	
}